<?php

namespace App\Http\Controllers\Admins;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admins\MenuRequest;
use App\Repositories\BLL\MenuBLL;

class MenuController extends Controller
{
	private $menuBLL;
	public function __construct(MenuBLL $menuBLL){
		$this->menuBLL = $menuBLL;
	}
    /**
     * 显示首页
     */
    public function index()
    {
    		//菜单选项
    		$pMenu = $this->menuBLL->findWhere([['level','<',config('admins.globals.menu_level')]]);
//		dd($pMenu);
		$menuList = $this->menuBLL->getMenuList();
//		dd($menuList);
        return view('admin.menu.index',
						['pmenu' => $pMenu,
						'menulist'=>$menuList]
					);
    }
	
	/**
     * 保存一个新的菜单。
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(MenuRequest $request)
    {
		$result = $this->menuBLL->create($request->all());
		if($result){
			//更新缓存
			$this->menuBLL->getMenuListCache();
			flash('添加成功!','success');
		}else{
			flash('添加失败!','danger');
		}
		return redirect('admin/menu');
    }
	
	/** 
     * 编辑
     * 
     * @param  int  $id 
     * @return Response 
     */  
    public function edit($id)  
    {
    		$menu = $this->menuBLL->editMenu($id);
        return response()->json($menu);
    }  
	
	/** 
     * 更新
     * 
     * @param  int  $id 
     * @return Response 
     */  
    public function update(MenuRequest $request)  
    {  
        $this->menuBLL->updateMenu($request);
        return redirect('admin/menu');
    } 
	
	/**
	 * 删除
	 */
	public function destroy($id){
        $this->menuBLL->destroyMenu($id);
        return redirect('admin/menu');
    }
	
}
